/*
 * @Author: tomystery
 * @Date:   2017-09-15 14:57:42
 * @Last Modified by:   tomystery
 * @Last Modified time: 2017-09-28 20:52:46
 */
'use strict';
var _mm = require('util/mm.js');
var _cities = require('util/cities/index.js');
var _address = require('service/address-service.js');
var templateAddressModal = require('./address-modal.string');


var addressModal = {
    show: function(option) {
        //option的绑定 isUpdate =false与onSuccess(){loadAddressList} 
        this.option = option;
        this.option.data=option.data || {};
        
        this.$modalWrap = $('.modal-wrap');
        //渲染页面
        this.loadModal();
        //绑定事件
        this.bindEvent();
    },
    //关闭弹窗
    hide: function() {
        this.$modalWrap.empty();
    },
    bindEvent:function(){
    	var _this=this;
    	//省市二级联动
    	this.$modalWrap.find('#receiver-province').change(function(){
    		//获取provinceName
    		var provinceSelected=$(this).val();
           
    		_this.loadCities(provinceSelected);
    	}),
        //提交表单
        // this.$modalWrap.find('.address-btn').click(function(){})
       this.$modalWrap.find('.address-save').click(function(){
            var receiverInfo=_this.getReceiverInfo()
          
             var  isUpdate   =_this.option.isUpdate ;
                //使用新地址，且验证通过
                if(!isUpdate && receiverInfo.status){
                    _address.save(receiverInfo.data,function(){
                        _mm.successTips('地址添加成功');

                        _this.hide();//弹框隐藏
                        typeof _this.option.onSuccess==='function'  
                        && _this.option.onSuccess(); //判断是不是函数，是的话就&&后面的

                    },function(errMsg){
                        _mm.errorTips(errMsg);

                    });

                }
                //修改地址并且验证通过
                else if(isUpdate && receiverInfo.status){
                   _address.update(receiverInfo.data,function(){
                        _mm.successTips('地址修改成功');

                        _this.hide();//弹框隐藏
                        typeof _this.option.onSuccess==='function'  
                        && _this.option.onSuccess(); //判断是不是函数，是的话就&&后面的

                    },function(errMsg){
                        _mm.errorTips(errMsg);

                    });
                }
                //验证不通过
                else{
                    _mm.errorTips(receiverInfo.errMsg||'好像哪里不对了');

                }
        });
        //保证点击modal内容区的的时候不关闭弹窗
        this.$modalWrap.find('.modal-container').click(function(e){
            e.stopPropagation();
        });
        //点击叉号或者蒙版区关闭弹窗
        this.$modalWrap.find('.close').click(function(){
            _this.hide();
        });

    },
    loadModal: function() {
      //渲染页面
        var addressModalHtml = _mm.renderHtml(templateAddressModal, {
          isUpdate:this.option.isUpdate,
          data    :this.option.data
        }); 

        this.$modalWrap.html(addressModalHtml); //页面挂载
        //加载省份
        this.loadProvince();
        
    },
    //加载省份信息
    loadProvince: function() {
      var _this=this;
        var provinces = _cities.getProvinces() || [];
        var $provinceSelect = this.$modalWrap.find('#receiver-province');
        	$provinceSelect.html(this.getSelectOption(provinces));
          //如果是更新地址，并且有省份信息，做省份信息的回填
          if(this.option.isUpdate && this.option.data.receiverProvince){
            $provinceSelect.val(this.option.data.receiverProvince);
            _this.loadCities(this.option.data.receiverProvince);
          }

    },
    loadCities: function(provinceName) {
    	var cities=_cities.getCities(provinceName) || [];
     
    	var $citySelect = this.$modalWrap.find('#receiver-city');
        	$citySelect.html(this.getSelectOption(cities));

           //如果是更新地址，并且有城市信息，做城市的回填
          if(this.option.isUpdate && this.option.data.receiverCity){
            
            $citySelect.val(this.option.data.receiverCity);
          }

    },
    //获取表单里面收件人信息，并作表单的验证
    getReceiverInfo:function(){
    
      var  receiverInfo ={},
            result      ={
            status:false
      };
      receiverInfo.receiverName=$.trim(this.$modalWrap.find('#receiver-name').val());
      receiverInfo.receiverProvince=$.trim(this.$modalWrap.find('#receiver-province').val());
      receiverInfo.receiverCity=$.trim(this.$modalWrap.find('#receiver-city').val());
      receiverInfo.receiverPhone=$.trim(this.$modalWrap.find('#receiver-phone').val());
      receiverInfo.receiverAddress=$.trim(this.$modalWrap.find('#receiver-address').val());
      receiverInfo.receiverZip=$.trim(this.$modalWrap.find('#receiver-zip').val());
      if(this.option.isUpdate){
        receiverInfo.id=this.$modalWrap.find('#receiver-id').val();

      }
      //下面是表单的验证
      if(!receiverInfo.receiverName){
        result.errMsg="请填写收件人姓名！";
      }else if(!receiverInfo.receiverProvince){
         result.errMsg="请选择收件人所在省份！";
      }else if(!receiverInfo.receiverCity){
        result.errMsg="请选择收件人所在的城市！";
      }else if(!receiverInfo.receiverPhone){
        result.errMsg="请选择收件人电话号码！";
      }else if(!receiverInfo.receiverAddress){
         result.errMsg="请选择收件人地址！";
      }else if(!receiverInfo.receiverZip){
            result.errMsg="请选择收件人邮编！";
      }
      //如果全部验证通过
      else{
        result.status=true;
        result.data=receiverInfo;
      }
      return result;

    },
    //获取select框中省，市的内容
    getSelectOption : function(optionArray){
        var html = '<option value="">请选择</option>';
        for(var i = 0, length = optionArray.length; i < length; i++){
            html += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>';
        }
        return html;
    },
};
module.exports = addressModal;